//
//  UtilsController.m
//  Unity-iPhone
//
//  Created by aby.wang on 2020/8/6.
//

#import "UtilsController.h"
#import <QQSdkTool/QQSdkUtils.h>

@implementation UtilsController

+ (instancetype)instance{
    static UtilsController *manager=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [UtilsController new];
    });
    return manager;
}

extern "C"{
//有回调
void SendEMailCallBack(char *gameObjectName,char *gameFuncName, char* recipients, char* subject, char* productName){
    NSString *object = [[NSString stringWithUTF8String:gameObjectName] copy];
    NSString *func = [[NSString stringWithUTF8String:gameFuncName] copy];
    QQSdkUtils *util = [QQSdkUtils shareUtils];
    UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    [util sendEmail:rootViewController emailAddress:[NSString stringWithUTF8String:recipients] title:[NSString stringWithUTF8String:subject] productName:[NSString stringWithUTF8String:productName]];
    util.sendEmailSuccess = ^{
        UnitySendMessage([object UTF8String], [func UTF8String], "0");
    };
    util.sendEmailFailure = ^{
        UnitySendMessage([object UTF8String], [func UTF8String], "2");
    };
}
//有回调
void SaveToAlbumCallBack(char *gameObjectName,char *gameFuncName ,char *path, int type){
    NSString *object = [[NSString stringWithUTF8String:gameObjectName] copy];
    NSString *func = [[NSString stringWithUTF8String:gameFuncName] copy];
    QQSdkUtils *util = [QQSdkUtils new];
    [util saveToAlbumNew:[NSString stringWithUTF8String:path] andMediaType:type  complete:^(BOOL isSuccess, NSString * _Nullable assetLocalID, NSError * _Nullable error) {
        if(isSuccess){
            UnitySendMessage([object UTF8String], [func UTF8String], "0");
        }else{
            UnitySendMessage([object UTF8String], [func UTF8String], "2");
        }
    }];
}

//有回调
void ShareToInstagramCallBack(char *gameObjectName,char *gameFuncName ,char *path){
    NSString *object = [[NSString stringWithUTF8String:gameObjectName] copy];
    NSString *func = [[NSString stringWithUTF8String:gameFuncName] copy];
    QQSdkUtils *util = [QQSdkUtils new];
    [util shareToInstagram:[NSString stringWithUTF8String:path] complete:^(BOOL isSuccess, NSError * _Nullable error) {
        if(isSuccess){
            UnitySendMessage([object UTF8String], [func UTF8String], "0");
        }else{
            if (error){
                UnitySendMessage([object UTF8String], [func UTF8String], "2");
            }else{
                UnitySendMessage([object UTF8String], [func UTF8String], "1");
            }
        }
    }];
}

//有回调
void RequestCameraPermissionCallBack(char *gameObjectName,char *gameFuncName){
    NSString *object = [[NSString stringWithUTF8String:gameObjectName] copy];
    NSString *func = [[NSString stringWithUTF8String:gameFuncName] copy];
    QQSdkUtils *util = [QQSdkUtils new];
    [util requestCameraPermission:^(BOOL hasPermisssion) {
        if(hasPermisssion){
            UnitySendMessage([object UTF8String], [func UTF8String], "0");
        }else{
            UnitySendMessage([object UTF8String], [func UTF8String], "2");
        }
    }];
}
//有回调
void RequestAlbumPermissionCallBack(char *gameObjectName, char *gameFuncName){
    NSString *object = [[NSString stringWithUTF8String:gameObjectName] copy];
    NSString *func = [[NSString stringWithUTF8String:gameFuncName] copy];
    QQSdkUtils *util = [QQSdkUtils new];
    [util requestAlbumPermission:^(BOOL hasPermisssion) {
        if(hasPermisssion){
            UnitySendMessage([object UTF8String], [func UTF8String], "0");
        }else{
            UnitySendMessage([object UTF8String], [func UTF8String], "2");
        }
    }];
}
void RequestIDFAPermissionCallBack(char *gameObjectName, char *gameFuncName){
        NSString *object = [[NSString stringWithUTF8String:gameObjectName] copy];
        NSString *func = [[NSString stringWithUTF8String:gameFuncName] copy];
        QQSdkUtils *util = [QQSdkUtils new];
        [util requestIDFAPermission:^(BOOL hasPermisssion) {
            if(hasPermisssion){
                UnitySendMessage([object UTF8String], [func UTF8String], "0");
            }else{
                UnitySendMessage([object UTF8String], [func UTF8String], "2");
            }
        }];
    }
}

@end
